﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using $myprojectname$.Infrastructure.Abstract.Providers;
using $myprojectname$.Mvc.Models;
using $myprojectname$.Domain.Abstract;

namespace $myprojectname$.Mvc.Controllers
{
    /// <summary>
    /// Account controller class.
    /// </summary>
    [$myprojectname$.Infrastructure.Filters.HandleError(View = "Error")]
    public class AccountController : Controller
    {
        IAuthenticationProvider authProvider;
        IProductRepository repository;

        public AccountController(IAuthenticationProvider auth, IProductRepository repository)
        {
            this.authProvider = auth;
            this.repository = repository;
        }

        public ViewResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (authProvider.Authenticate(model.UserName, model.Password))
                {
                    return Redirect(returnUrl ?? Url.Action("List", "Product"));
                }
                else
                {
                    ModelState.AddModelError("", "Incorrect username or password");
                    return View();
                }
            }
            else
            {
                return View();
            }
        }

        public ActionResult Logout()
        {
            authProvider.SignOut();

            return Redirect(Url.Action("Login"));
        }
    }
}
